package com.gxa.jjty.admin.schedule;

import com.gxa.jjty.admin.service.EvaluateService;
import com.gxa.jjty.admin.service.HandicappedService;
import com.gxa.jjty.admin.service.OrdersetService;
import com.gxa.jjty.admin.service.TorderService;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.Handicapped;
import com.gxa.jjty.core.pojo.Orderset;
import com.gxa.jjty.core.pojo.Torder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author JJuventus
 * @date 2021/11/12 14:25
 */

@Component
public class ChangeOrderState {
    @Autowired
    private TorderService torderService;
    @Autowired
    OrdersetService ordersetService;
    @Autowired
    HandicappedService handicappedService;
    @Autowired
    EvaluateService evaluateService;

    //付款状态
    @Scheduled(fixedRateString = "300000")
    public void selectPayState() throws IllegalAccessException {
        Orderset orderset = ordersetService.selectAll().get(0);
        Torder torder = new Torder();
        torder.setOrderState("待付款");
        List<Torder> select = torderService.select(torder);
        if (select.size() > 0) {
            for (Torder torder1 : select) {

                //后边删除这段:数据残缺处理,订单时间没有给加一个当前时间
                //////////////////////////////////////////////////////////////////
                if(torder1.getOrderTime()==null){
                    torder1.setOrderTime(new Date());
                    torderService.updateById(torder1);
                }
                //////////////////////////////////////////////////////////////////

                //订单状态为待付款,当前时间-订单创建时间>过期分钟,则改变状态为取消,添加取消时间
                if ((System.currentTimeMillis() - torder1.getOrderTime().getTime()) / (1000 * 60)
                        >= orderset.getPayOvertime() * 1000 * 60) {
                    torder1.setOrderState("已取消");
                    torder1.setCancelFrom(0);
                    torder1.setCancelTime(new Date());
                    torderService.updateById(torder1);
                }
            }
        }
    }

    //分配状态
    @Scheduled(fixedRateString = "300000")
    public void selectAllotState() throws IllegalAccessException, BusinessException {
        Orderset orderset = ordersetService.selectAll().get(0);
        Torder torder = new Torder();
        torder.setOrderState("等待系统分配");
        List<Torder> select = torderService.select(torder);
        if (select.size() > 0) {
            for (Torder torder1 : select) {

                //后边删除这段:数据残缺处理,订单支付时间没有给加一个当前时间
                //////////////////////////////////////////////////////////////////
                if(torder1.getPayTime()==null){
                    torder1.setPayTime(new Date());
                    torderService.updateById(torder1);
                }
                //////////////////////////////////////////////////////////////////

                //订单状态为等待系统分配,当前时间-支付时间>过期分钟,则改变状态为取消,添加取消时间
                if ((System.currentTimeMillis() - torder1.getPayTime().getTime())
                        >= (orderset.getAllotOvertime() * 1000 * 60)) {
                    torder1.setOrderState("已取消");
                    torder1.setCancelFrom(1);
                    torder1.setCancelTime(new Date());
                    torderService.updateById(torder1);
                }
            }
        }
    }



    //评价状态
    @Scheduled(fixedRateString = "300000")
    public void selectPraise() throws IllegalAccessException, BusinessException {
        Orderset orderset = ordersetService.selectAll().get(0);
        Torder torder = new Torder();
        torder.setOrderState("已完成");
        //查询所有已完成订单
        List<Torder> select = torderService.select(torder);
        List<Evaluate> evaluates = evaluateService.selectAll();
        for (Evaluate evaluate : evaluates) {
            //排除已经评价过的订单
            Torder torder1 = torderService.selectById(evaluate.getOrderId());
            select.remove(torder1);
        }
        //剩下的已完成订单判断评价时间是否超时:
        if(select.size()==0){
            //全部评价了,直接返回
            return;
        }
        for (Torder torder1 : select) {
            if (torder1.getFlag() == -1 && (System.currentTimeMillis() - torder1.getServfinishTime().getTime())
                    >= (orderset.getAutomaticPraise() * 1000 * 60 * 60 * 24)) {
                Evaluate evaluate = new Evaluate();
                evaluate.setOrderId(torder1.getOrderId());
                evaluate.setScore("非常好");
                evaluate.setRemark("默认好评");
                evaluateService.insert(evaluate);
                torder1.setFlag(3);
                torderService.updateById(torder1);
            }
        }

    }



    //退款状态
    @Scheduled(fixedRateString = "300000")
    public void selectRefundState() throws IllegalAccessException {
        Orderset orderset = ordersetService.selectAll().get(0);
        Torder torder = new Torder();
        torder.setOrderState("退款中");
        List<Torder> select = torderService.select(torder);
        if (select.size() > 0) {
            for (Torder torder1 : select) {

                //后边删除这段:数据残缺处理,订单取消时间没有给加一个当前时间
                //////////////////////////////////////////////////////////////////
                if(torder1.getCancelTime()==null){
                    torder1.setCancelTime(new Date());
                    torderService.updateById(torder1);
                }
                //////////////////////////////////////////////////////////////////


                //订单状态退款中,得到间隔时间,判断是否超时
                if ((System.currentTimeMillis() - torder1.getCancelTime().getTime())
                        >= (orderset.getRefundOvertime() * 1000 * 60 * 60)) {
                    torder1.setOrderState("退款成功");
                    torderService.updateById(torder1);

                    //这里需要有一个退款到用户账户的方法
                    Handicapped handicapped = new Handicapped();
                    handicapped.setId(torder1.getCjrId());
                    handicapped.setMoney(handicapped.getMoney()+torder1.getPayPrice());
                    handicappedService.updateById(handicapped);
                }
            }
        }
    }

    //完成状态
    @Scheduled(fixedRateString = "300000")
    public void selectServeFinishState() throws IllegalAccessException {
        Orderset orderset = ordersetService.selectAll().get(0);
        Torder torder = new Torder();
        torder.setOrderState("服务中");
        List<Torder> select = torderService.select(torder);
        if (select.size() > 0) {
            for (Torder torder1 : select) {
                //订单状态为待付款,当前时间-订单创建时间>过期分钟,则改变状态为取消,添加取消时间
                if ((System.currentTimeMillis() - torder1.getServeTime().getTime())
                        >= (orderset.getServefinishOvertime() * 1000 * 60 * 60)) {
                    torder1.setOrderState("已完成");
                    torder1.setServfinishTime(new Date());
                    torderService.updateById(torder1);
                }
            }
        }
    }

    //筛选所有超时状态
    public void changeAllOverTimeOrderState() throws Exception {
        selectPayState();
        selectAllotState();
        selectPraise();
        selectRefundState();
        selectServeFinishState();
    }
}
